package com.xiaoma.tpo.dao;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.domain.Messages;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Owner on 2015/4/14.
 */
@Repository
public class MessagesDao extends BaseDaoImpl<Messages> {
    public List<Messages> getMessages(int userid,int page,int status){
        StringBuffer sb = new StringBuffer();
        sb.append("select id,content,status,date_time,user_id from messages where user_id=? ");
        if(status>0){
            sb.append(" and status=");
            sb.append(status);
        }
        sb.append(" order by date_time desc limit ?,?");
        List<?> list = jdbcTemplate.query(sb.toString(), new DefaultRowMapper(Messages.class, getNameHandler()), userid,(page-1)*10,10);
        return (List<Messages>)list;
    }

    public void updateStatus(int msgId){
        String sql = "update messages set status=2 where id="+msgId;
        jdbcTemplate.execute(sql);
    }
}
